package com.sysu.minsysu.zxcx;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.sysu.minsysu.R;
import com.sysu.minsysu.xyxw.XiaoYuanXiaoWu;
import com.sysu.minsysu.xyxw.YiDongXueXi;

public class ZiXunChaXun extends Activity{

	SimpleAdapter mAdapter;
	GridView mGridView;
	LinearLayout mIconLayout;
	ImageView[] mIconViews;
	
	int[] iconRes = new int[]{
			R.drawable.zixungongxiang_bbs,
			R.drawable.zixungongxiang_news,
			R.drawable.zixungongxiang_yidongxuexi,
	};
	
	@Override
	public void onCreate( Bundle savedInstanceState ){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gridmainlayout);
		
		mGridView = (GridView)findViewById(R.id.gridview);
		mIconLayout = (LinearLayout)findViewById(R.id.icon_title);
		
		mAdapter = new SimpleAdapter(this, getData(), R.layout.view_appmain, new String[]{ "image", "text" }, new int[]{R.id.imageView,R.id.textView});
		mGridView.setAdapter(mAdapter);
		mGridView.setNumColumns(2);
		mGridView.setOnItemClickListener(new MyOnItemClickListener());
		setIconTitle();
	}
	
	@Override
	public boolean onKeyDown( int keyCode, KeyEvent event){
		if( keyCode==KeyEvent.KEYCODE_BACK)
			return false;
		else return super.onKeyDown(keyCode, event);
	}
	
	int mIconCount;
	private void setIconTitle(){
		mIconCount = 3;
		mIconViews = new ImageView[mIconCount];
		switch (mIconCount) {
		case 6: mIconViews[5] = (ImageView)mIconLayout.findViewById(R.id.icon5);
		case 5: mIconViews[4] = (ImageView)mIconLayout.findViewById(R.id.icon4);
		case 4: mIconViews[3] = (ImageView)mIconLayout.findViewById(R.id.icon3);
		case 3: mIconViews[2] = (ImageView)mIconLayout.findViewById(R.id.icon2);
		case 2: mIconViews[1] = (ImageView)mIconLayout.findViewById(R.id.icon1);
		case 1: mIconViews[0] = (ImageView)mIconLayout.findViewById(R.id.icon0);
			break;
		}
		
		for( int i=0; i<mIconCount; i++ ){
			mIconViews[i].setVisibility(View.VISIBLE);
			mIconViews[i].setOnClickListener( new MyTitleIconClickListener(i) );
			mIconViews[i].setImageResource(iconRes[i]);
		}
	}
	
	private class MyTitleIconClickListener implements OnClickListener{

		int num;
		public MyTitleIconClickListener( int n ){
			num=n;
		}
		@Override
		public void onClick(View v) {
			mGridView.performItemClick(null, num, 0);
		}
		
	}

	private class MyOnItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			if( arg2==1){
				Intent intent = new Intent( ZiXunChaXun.this, NewsMain.class);
				startActivity(intent);
			}
			if( arg2==0){
				Intent intent = new Intent( ZiXunChaXun.this, BBSMain.class);
				//intent.putExtra("type", "bbs");
				startActivity(intent);
			}
			if( arg2==2 ){
				Intent intent = new Intent(ZiXunChaXun.this, YiDongXueXi.class);
				startActivity(intent);
				return;
			}
		}
	}
	
	private List< Map<String, Object> > getData(){
			List< Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("image", iconRes[0]);
			map.put("text", "BBS论坛");
			list.add(map);
			
			map = new HashMap<String, Object>();
			map.put("image", iconRes[1]);
			map.put("text", "中大新闻");
			list.add(map);
			
			map = new HashMap<String, Object>();
			map.put("image", iconRes[2]);
			map.put("text", "移动学习");
			list.add(map);
		
			return list;
		}
}
